﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace SIGEB.Modelo.moduloNotificaciones
{
    public class WebMail
    {
        private string smtp;
        private int port;

        public WebMail()
        {
            smtp = string.Empty;
            port = -1;
        }

        public String Host(String email)
        {
            bool write = false;
            String STMP = "";
            foreach (Char a in email)
            {
                if (write && a == '.')
                {
                    write = false;
                }
                if (write)
                {
                    STMP += a;
                }
                if (a == '@')
                {
                    write = true;
                }
            }
            return STMP;
        }

        private string subString(string input)
        {
            int i1 = input.IndexOf("@");
            if (i1 != -1)
            {
                int i2 = input.LastIndexOf(".");
                if (i2 != -1)
                {
                    int start = i1 + "@".Length;
                    int len = i2 - start;
                    return input.Substring(start, len);

                }
            }
            return string.Empty;
        }

        public bool EnviaEmail(String mensaje, String asunto, bool html, String destino, String from, String contrasena)
        {
            try
            {
                MailMessage correo = new MailMessage();
                SmtpClient envio = new SmtpClient();
                NetworkCredential autenticar = new NetworkCredential();

                string s1 = subString(from);
                switch (s1)
                {
                    case "hotmail": smtp = "live";
                                    port = 587;
                                    break;
                    case "gmail": smtp = "gmail";
                                    envio.EnableSsl = true;
                                    port = 465;
                                    break;
                    case "yahoo": smtp = "mail.yahoo";
                                    port = 587;
                                    break;
                    default: return false;
                }

                correo.Body = mensaje;
                correo.Subject = asunto;
                correo.IsBodyHtml = html;
                correo.To.Add(destino);
                correo.From = new MailAddress(from, "Biblioteca CMI");
                envio.Credentials = new NetworkCredential(from, contrasena);
                envio.Host = "smtp." + smtp + ".com";
                envio.Port = port;
                //envio.EnableSsl = true; //gmail trabaja con ssl asi que lo habilitamos
                try
                {
                    envio.Send(correo);
                    return true;
                }
                catch (SmtpException exc)
                {
                    return false;
                }
                
            }
            catch
            {
                throw new Exception("Ha ocurrido un error enviando el correo.");
            }
        }

        public bool EnviaEmail(String mensaje, String asunto, bool html, LinkedList<string> destinos, String from, String contrasena)
        {
            //try
            //{
                MailMessage correo = new MailMessage();
                SmtpClient envio = new SmtpClient();
                NetworkCredential autenticar = new NetworkCredential();

                string s1 = subString(from);
                switch (s1)
                {
                    case "hotmail": smtp = "live";
                        port = 587;
                        break;
                    case "gmail":
                        {
                            envio.EnableSsl = true;
                            smtp = "gmail";
                            port = 465;
                            break;
                        }
                    case "yahoo": smtp = "mail.yahoo";
                        port = 587;
                        break;
                    default: return false;
                }

                
                correo.Body = mensaje;
                correo.Subject = asunto;
                correo.IsBodyHtml = html;
                if (destinos != null)
                {
                    foreach (string ema in destinos)
                    {
                        correo.To.Add(ema);
                    }
                }
                correo.From = new MailAddress(from, "Biblioteca CMI");
                envio.Credentials = new NetworkCredential(from, contrasena);
                envio.Host = "smtp."+ smtp + ".com";
                envio.Port = port;
                //envio.EnableSsl = true; //gmail trabaja con ssl asi que lo habilitamos
                try
                {
                    envio.Send(correo);
                    return true;
                }
                catch(SmtpException exc)
                {
                    return false;
                }
            //}
            //catch
            //{
            //    throw new Exception("Ha ocurrido un error enviando el correo.");
            //}
        }
    }
}
